package com.yunyouyi.grabtickets.redis;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public Long decrStock(String key) {
        return stringRedisTemplate.opsForValue().decrement(key);
    }

    public void setStock(String key, Integer value) {
        stringRedisTemplate.opsForValue().set(key, value.toString());
    }

    public Integer getStock(String key) {
        String value = stringRedisTemplate.opsForValue().get(key);
        return value == null ? null : Integer.parseInt(value);
    }
}